package com.talent.web.excel;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.converters.longconverter.LongStringConverter;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.excel.write.merge.LoopMergeStrategy;
import com.talent.web.excel.model.DataDemo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName：ExcelReadServiceImpl
 * @Author: hszhou
 * @Date: 2024/11/16 11:35
 * @Description: 必须描述类做什么事情, 实现什么功能
 */
@Service
public class ExcelReadServiceImpl implements ExcelReadService{


    private static List<DataDemo> data=new ArrayList<>();

    static {

        DataDemo dataDemo = new DataDemo();
        dataDemo.setBirthday(LocalDateTime.now());

        dataDemo.setSex(1);

        data.add(dataDemo);
    }

    protected  final Logger logger = LoggerFactory.getLogger(getClass());
    @Override
    public void readSimpleExcel(MultipartFile file) {
       try {
           InputStream inputStream = file.getInputStream();
           //第一种方式
//           EasyExcel.read(inputStream, DataDemo.class,new PageReadListener<DataDemo>(items->{
//               for (DataDemo item : items) {
//                   logger.info("读到一条数据{}",item);
//               }
//           })).sheet().doRead();
           //第二种方式
           EasyExcel.read(inputStream, DataDemo.class,new SimpleReadListener()).sheet().doRead();
       }catch (Exception e){

       }

    }

    @Override
    public void readAllSheet(MultipartFile file ) {
        try {
            InputStream inputStream = file.getInputStream();
            EasyExcel.read(inputStream, DataDemo.class,new SimpleReadListener()).doReadAll();
        }catch (Exception e){

        }
    }

    @Override
    public void readIndexSheet(int[] indexArr,MultipartFile file) {
        try (ExcelReader excelReader = EasyExcel.read(file.getInputStream()).build()){
            ReadSheet[] readSheets = new ReadSheet[indexArr.length];
            for (int index : indexArr) {
                ReadSheet build = EasyExcel.readSheet(index).head(DataDemo.class).registerReadListener(new SimpleReadListener()).build();
                readSheets[index]=build;
            }
            excelReader.read(readSheets);
        }catch (Exception e){

        }
    }

    @Override
    public void readMultiHead(MultipartFile file) {

        try {
            InputStream inputStream = file.getInputStream();
            EasyExcel.read(inputStream, DataDemo.class,new SimpleReadListener()).sheet()
                    .headRowNumber(2)
                    .doRead();
        } catch (IOException e) {


        }
    }

    @Override
    public void writeToDisk(String path) {

        EasyExcel.write(path, DataDemo.class)
                .sheet("sheet1")
                .doWrite(data);
    }

    @Override
    public void download(HttpServletResponse response) {

        LoopMergeStrategy loopMergeStrategy = new LoopMergeStrategy(2, 0);
        try {
            EasyExcel.write(response.getOutputStream(),DataDemo.class)
                    .autoCloseStream(Boolean.FALSE)
                    //解决Long类型的精度丢失问题
                    .registerConverter(new LongStringConverter())
                    //合并单元格
                    .registerWriteHandler(loopMergeStrategy)
                    .sheet(0)
                    .doWrite(data);
        }catch (Exception e){

            e.printStackTrace();
        }
    }
}
